package servlet;

import entity.PersonService;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.Utilities;

/**
 *
 * @author Francis
 * @version
 */
public class ChangePasswordServlet extends HttpServlet {

    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String oldpassword = request.getParameter("password1");
        String password = request.getParameter("password9");
        String repassword = request.getParameter("repassword");
        Integer personID = (Integer) request.getSession().getAttribute("personID");
        /*String captchaCode = request.getParameter("captchacode");
        boolean isCorrectCaptcha = false;
        
        try {
        isCorrectCaptcha = CaptchaServiceSingleton.getInstance().validateResponseForID(session.getId(), captchaCode);
        } catch (CaptchaServiceException e) {
        //should not happen, may be thrown if the id is not valid
        }*/
        boolean isCorrectCaptcha = true;
        if (!isCorrectCaptcha) {
            response.sendRedirect("ChangePassword.jsp?msg=1");
            return;
        } else if (Utilities.isInvalid(password) || password.length()< 6 || !password.equals(repassword)) {
            response.sendRedirect("ChangePassword.jsp?msg=1");
            //todo:password mismatch
            return;
        } else {
            boolean succeed = PersonService.updatePassword(personID, oldpassword, repassword);
            //boolean succeed = MemberBean.updatePassword(username, oldpassword, password, repassword);

            if (succeed) {
                response.sendRedirect("UpdatePasswordSuccess.jsp");
                return;
            } else {
                //user not found, or old passwords mismatch
                response.sendRedirect("UpdatePasswordFail.jsp");
                return;
            }
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
